<?php

namespace app\admin\controller;

use app\admin\model\AdminUsers as Users;
use app\admin\model\AuthGroup as Group;
/**
 * APP 后台管理者用户管理
 * 2017年5月31日 17:32:41
 * @吴家奕 陈润璇
 */
class AdminUser extends Baseic
{
    public function __auto(){
      $this->model   = new Users();
    }

   /**
     * get admin/AdminUser/index
     * 显示管理者列表视图
     * @return view
     */
    public function index()
    {
        $data = $this->model->getAllUser();
        $this->assign('data',$data);
        $this->assign('userId',$this->userId);
        return view();
    }

     /**
     * get/post admin/AdminUser/create
     * 显示后台视图
     * @return view
     */
    public function create()
    {
      if ($this->request->isPost()) {
         $data = input('post.');

         // 查询手机是否已注册
         if($this->model->checkIsUser(['key' => $data['phone']])){
          return  json(['state'=>false,'msg'=>"该手机已注册"]);
         }
         // 查询邮箱是否存在
         if($this->model->checkIsUser(['key' => $data['email']])){
          return  json(['state'=>false,'msg'=>"该邮箱已注册"]);
         }

         // 组合数据
         unset($data['password_d']);
         $data['register_time'] = time();
         $data['password'] = encrypt_pass(input('post.password'));

         $re   = $this->model->createUser($data);
         if ($re) {
           $result = ['state'=>true,'msg'=>"新增成功"];
         }else {
           $result = ['state'=>false,'msg'=>"服务器繁忙"];
         }
         unset($data);
         unset($this->model);
         unset($re);
         return $result;
       }

       $db   = new Group();
       $data = $db->getAllGroup();
       $this->assign('group',$data);
       unset($data);
       unset($this->model);
       unset($db);
       return view();
    }


    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit()
    {
      if ($this->request->isPut()) {
         $data = input('put.');
         $uid = input('put.id');

         // 如果修改手机-查询手机是否已注册
         if(cache('phone'.$uid) != $data['phone'] && $this->model->checkIsUser(['key' => $data['phone']])){
          return  json(['state'=>false,'msg'=>"该手机已注册"]);
         }

         // 组合数据
         if(!session('is_admin') && $data['id'] != $this->userId ){
           unset($data['group_id']);
         }
         unset($data['id']);
         
         $re   = $this->model->editUserInfo($uid,$data);
         if ($re) {
           cache('phone'.$uid, NULL);
           $result = ['state'=>true,'msg'=>"修改成功"];
         }else {
           $result = ['state'=>false,'msg'=>"服务器繁忙"];
         }

         unset($data);
         unset($this->model);
         unset($re);
         return $result;
       }

       $uid = input('get.id');
       $userData = $this->model->getUserInfo($uid);
       cache('phone'.$uid,$userData['phone'],300);

       $this->assign('user',$userData);

       $db   = new Group();
       $data = $db->getAllGroup();
       $this->assign('group',$data);

       unset($this->model);
       unset($userData);
       unset($data);
       unset($db);
       return view();
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
    * delete admin/AdminUser/delete
    * 删除
    * @return json
    */
    public function delete()
    {
      if ($this->request->isDelete()) {

         if(!session('is_admin')){
           return ['state'=>false,'msg'=>"您不是最高权限用户,无法操作"];
         }

         $uid  = input('delete.id');
         $re   = $this->model->delUser($uid);
         if ($re) {
           $result = ['state'=>true,'msg'=>"成功"];
         }else {
           $result = ['state'=>false,'msg'=>"服务器繁忙"];
         }

         unset($data);
         unset($this->model);
         unset($re);

         return $result;
       }
    }

    /*
      delete
      批量删除
      @return json
    */
    public function allDelete()
    {
       if ($this->request->isDelete()) {
         if(!session('is_admin')){
           return ['state'=>false,'msg'=>"您不是最高权限用户,无法操作"];
         }
         
         $uid  = input('delete.');
         $id = implode(',',$uid['id']);
         $res = $this->model->deleteAll($id);
         if($res){
           $result = ['state'=>true,'msg'=>"成功"];
         }else{
           $result = ['state'=>false,'msg'=>"失败"];
         }
         return $result;
       }
    }
}
